<?php
declare(strict_types=1);

namespace App\Service;

use App\Model\Enum\StatusEnum;
use App\Utils\CustomUtils;
use Hyperf\Di\Annotation\Inject;
use App\Model\Dao\SysCardTypeInfoDao;
use App\Exception\XZXValidationException;
use App\Constants\XZXResponseCode;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Coroutine;
use App\Utils\DateUtils;
use App\Utils\ArrayUtils;


class SysCardTypeInfoService {
    /**
     * @Inject()
     * @var SysCardTypeInfoDao
     */
    private $SysCardTypeInfoDao;

    public function get ($param) {
        $result = $this->SysCardTypeInfoDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }
        if (isset($result['createTime'])) {
            $result['createTime'] = DateUtils::getDateTime($result['createTime']);
        }

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE, XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), $result);
    }

    public function del ($param) {
        $result = $this->SysCardTypeInfoDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        try {
            $result = $this->SysCardTypeInfoDao->del($param['id'], false);
        } catch (\Throwable $th) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DEL_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE, XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), CustomUtils::xzxEmptyObject());
    }

    public function add ($param) {

        $data               = $param;
        $data['id']         = CustomUtils::createUUID();
        $data['createTime'] = time();
        unset($data['uid']);
        CustomUtils::xzxDebug($data);

        try {
            $this->SysCardTypeInfoDao->insert($data);
        } catch (\Throwable $th) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::ADD_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE, XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), CustomUtils::xzxEmptyObject());
    }

    public function edit ($param) {
        $info = $this->SysCardTypeInfoDao->get($param['id']);
        if (!$info) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        $id   = $param['id'];
        $data = [];
        // isset($param['name']) && $data['name'] = $param['name'];
        // isset($param['desciption']) && $data['desciption'] = $param['desciption'];

        foreach ($param as $k => $v) {
            if ($k == "id") {
                continue;
            }
            if (isset($v)) {
                $data[$k] = $v;
            }
        }
        CustomUtils::xzxDebug($data);

        try {
            $this->SysCardTypeInfoDao->update($id, $data);
        } catch (\Throwable $th) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::EDIT_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE, XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), CustomUtils::xzxEmptyObject());
    }

    public function list (array $param) {

        $limit = $param['limit'];
        $page  = $param['page'];

        $total = $this->SysCardTypeInfoDao->getCount($param);
        $list  = $this->SysCardTypeInfoDao->getList($param, $page, $limit);

        CustomUtils::xzxDebug($list);
        foreach ($list as $k => $v) {
            CustomUtils::xzxDebug($v);
            if (isset($v['createTime'])) {
                $v['createTime'] = DateUtils::getDateTime($v['createTime']);
            }
            $list[$k] = $v;
        }

        $data = [
            'total' => $total,
            'list'  => $list,
        ];

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE, XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), $data);
    }

    public function getOption (array $param) {

        $list = $this->SysCardTypeInfoDao->getList($param);

        $options = array_map(function ($item) {
            return [
                'label' => $item['label'],
                'value' => $item['value'],
            ];
        }, $list);

        $data = $options;
        return CustomUtils::buildSuccess($data);
    }

    /**
     * 卡种字符串归一化
     *
     * @param $cardType string 用户输入卡种
     *
     * @return string
     * @throws \Exception
     */
    public function formatCardTypeOrFail (?string $cardType) {

        // 全局卡种白名单
        $cardTypeInfo = $this->SysCardTypeInfoDao->getWhere([
            'status' => StatusEnum::ON,
            'value'  => $cardType,
        ]);

        if (!$cardTypeInfo) {
            throw new \Exception(trans('pay.account.credit.not_support_type', ['type' => $cardType]));
        }

        return (string) $cardTypeInfo['label'];
    }
}
